#pragma once

#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

namespace zmh
{
    class Intaddr
    {
    public:
        Intaddr(const struct sockaddr_in &addr)
            : _addr(addr)
        {
            _ip = inet_ntoa(_addr.sin_addr);
            _port = ntohs(_addr.sin_port);
        }
        Intaddr(uint16_t family, std::string ip, uint16_t port)
            : _addr({0}),
              _ip(ip),
              _port(port)
        {

            _addr.sin_family = family;
            _addr.sin_addr.s_addr = inet_addr(ip.c_str());
            _addr.sin_port = htons(port);
        }
        Intaddr(uint16_t family, uint16_t port)
            : _addr({0}),
              _ip("0,0,0,0"),
              _port(port)
        {

            _addr.sin_family = family;
            _addr.sin_addr.s_addr = INADDR_ANY;
            _addr.sin_port = htons(port);
        }
        // 引用返回
        struct sockaddr_in &Addr() { return _addr; };
        // 值返回
        std::string Ip() const { return _ip; }
        uint16_t Port() const { return _port; }
        std::string Name() const { return _ip + "-" + std::to_string(_port); }

    private:
        struct sockaddr_in _addr;
        std::string _ip;
        uint16_t _port;
    };
}